<?php
/*
* 禁止浏览器进行缓存
*/
header("Expires: -1");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
session_start();//开启session会话
require_once("./PDO_class.php");//引入封装的PDO类
require_once("./security_procedures.php");//引入安全防护
require_once("./config.php");//引入配置文件
require_once("./func.php");//引入功能函数
$DB=new DB($dbconfig);//实例化PDO类，加载配置
/*
* 实例化RSA加密类
* 加载公钥秘钥
*/
$RSA=new RSA(file_get_contents('E:\gongju\xampp\htdocs\ceshi\creativehome\api\public.txt'),file_get_contents('E:\gongju\xampp\htdocs\ceshi\creativehome\api\private.txt'));
$ac=$_GET["ac"];//获取用户进行的操作
/*
* 根据用户进行的操作对数据进行增删改查
*/
switch($ac){
	case 'ITE'://编辑简介
		//new sqlsafe();
		if(checktoken($_GET["token"]))
		{
			$arrys=json_decode(file_get_contents("php://input"),true);
			$where=array('id'=>$arrys['id']);
			array_shift($arrys);
			$DB->update('brief_introduction',$arrys,$where);
			$where2=array('owned_User'=>$_SESSION['name']);
			$returndata=array($DB->querys('brief_introduction','',$where2),gettoken($_SESSION['id']));
			echo json_encode($returndata);
		}
		else
		{
			echo 403;
		}
		break;
	case 'loding'://session初始化
		new sqlsafe();
		$name=Data_filtering($_GET["name"]);
		$pass=Data_filtering($_GET["pass"]);
		$token=Data_filtering($_GET["token"]);
		$arry=explode(',',$RSA->public_decrypt(str_replace(' ','+', $token)));
		$_SESSION['id']=$arry[0];
		$_SESSION['name']=$arry[1];
		$_SESSION['pass']=$arry[2];
		$_SESSION['jurisdiction']=$arry[3];
		$_SESSION['authorize']=$arry[4];
		$_SESSION['EFA']=$arry[5];
		$_SESSION['issuer']=$arry[6];
		break;
	case 'sig'://登录，签发token令牌
		new sqlsafe();
		$name=Data_filtering($_GET["name"]);
		$pass=Data_filtering($_GET["pass"]);	
		$where=array('name'=>$name,'pass'=>$pass);
		$arry=array();
		$arry=$DB->query('user','',$where);
		if(sizeof($arry)!=0)
		{	
			if($arry['authorize']==0)
			{
				$token=gettoken($arry['id']);
				$_SESSION['token']=$token;
				$_SESSION['tokennum']=0;
				echo json_encode($token);
			}
			else
			{
				echo "USER_001";
			}
		}
		else
		{
			echo false;
		}
		break;
	case 'BS'://更新故事列表
		new sqlsafe();
		if(checktoken($_GET["token"]))
		{
			$where=array('owned_User'=>$_SESSION['name']);
			$arry=array();
			$arry=$DB->querys('brief_introduction','',$where);
			array_push($arry,gettoken($_SESSION['id']));
			echo json_encode($arry);
		}
		else
		{
			echo 403;
		}
		break;
	case 'NEW'://新建故事
		new sqlsafe();
		if(checktoken($_GET["token"]))
		{
			$arrys=json_decode(file_get_contents("php://input"),true);
			$arrys['owned_User']=$_SESSION['name'];
			$arry=array('storyid'=>$DB->insert("brief_introduction",$arrys),'token'=>gettoken($_SESSION['id']));		
			echo json_encode($arry);
		}
		else
		{
			echo 403;
		}
		break;
	case 'RD'://角色详情
		new sqlsafe();
		if(checktoken($_GET["token"]))
		{
			$roleid=Data_filtering($_GET["roleid"]);
			$where=array('id'=>$roleid);
			$arry=array($DB->query('role','',$where),'token'=>gettoken($_SESSION['id']));
			$where['id']=$arry[0]['camp'];
			if($DB->query('camp','',$where)==[])
			{
				$arry[0]['camp']='';
			}
			else
			{
				$arry[0]['camp']=$DB->query('camp','',$where)['campname'];
			}
			echo json_encode($arry);
		}
		else
		{
			echo 403;
		}
		break;
	case 'RSTALODING'://加载关系数据，属性数据
		new sqlsafe();
		if(checktoken($_GET["token"]))
		{
			$roleid=Data_filtering($_GET["roleid"]);
			$where=array('id'=>$roleid);
			$Pdata=array($DB->query('role','',$where));
			$Pdata=explode('|',$Pdata[0]['relationship']);
			$arry=array();
			$arry2=array();
			$arrys=array();
			$i=0;
			foreach($Pdata as $key=>$value)
			{
				if($key%2==0)
				{
					$arry[$i]['role_relationship']=$value;
				}
				else
				{
					$where=array('id'=>$value);
					$arry[$i]['id']=$value;
					$arry[$i]['name']=$DB->query('role','',$where)['name'];
					$i++;
				} 
			}
			$where['id']=$roleid;
			$Pdata=array($DB->query('role','',$where));
			$Pdata=explode('|',$Pdata[0]['attribute']);
			$i=0;
			$a=0;
			foreach($Pdata as $key=>$value)
			{
				if($key==$a)
				{
					$arry2[$i]['id']=$value;
				}
				else
				{
					if($key==$a+1)
					{
						$arry2[$i]['property_name']=$value;
					}
					if($key==$a+2)
					{
						$arry2[$i]['content']=$value;
						$a=$a+3;
						$i++;
					}
				} 
			}
			array_push($arrys,$arry,$arry2,gettoken($_SESSION['id']));
			echo json_encode($arrys);
		}
		else
		{
			echo 403;
		}
		break;
	case 'newroleloding'://初始化新建角色
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$storyid=Data_filtering($_GET['storyid']);
			$where=array('the_story'=>$storyid);
			$arry=array($DB->querys('camp','',$where),gettoken($_SESSION['id']));
			echo json_encode($arry);
		}
		break;
	case 'newrole'://添加新角色
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$arrys=json_decode(file_get_contents("php://input"),true);
			if($arrys['data']['lead'])
			{
				$arrys['data']['lead']='√';
			}
			else
			{
				$arrys['data']['lead']='×';
			} 
			$DB->insert('role',$arrys['data']);
			$where=array('the_story'=>$arrys['data']['the_story']);
			$arry=array($DB->querys('role','',$where),gettoken($_SESSION['id']));
			$data=$DB->querys('role','',$where);
			$rolenum=count($data);
			$updata=array('rolenum'=>$rolenum);
			$where2=array('id'=>$arrys['data']['the_story']);
			$DB->update('brief_introduction',$updata,$where2);
			echo json_encode($arry);
		}
		else
		{
			echo 403;
		}
		break;
	case 'addrelationshiploding'://初始化新建关系角色列表
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$where=array('the_story'=>$_GET['the_story']);
			$data=array($DB->querys('role','',$where,'id,name'),gettoken($_SESSION['id']));
			echo json_encode($data);
		}
		else
		{
			echo 403;
		}
		break;
	case 'addrelationship'://添加新关系
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$arrys=json_decode(file_get_contents("php://input"),true);
			$where=array('id'=>$arrys['roleid']);
			$data=$DB->query('role','',$where,'relationship');
			if(strstr($data['relationship'],$arrys['data']['to_who'])==false)
			{
				if($data['relationship']=='')
				{
					$data['relationship']=$arrys['data']['relationship'].'|'.$arrys['data']['to_who'];
				}
				else
				{
					$data['relationship']=$data['relationship'].'|'.$arrys['data']['relationship'].'|'.$arrys['data']['to_who'];
				}
				$DB->update('role',$data,$where);
				$arry=array();
				$Pdata=array($DB->query('role','',$where,'relationship')['relationship']);
				$Pdata=explode('|',$Pdata[0]);
				$i=0;
				foreach($Pdata as $key=>$value)
				{
					if($key%2==0)
					{
						$arry[$i]['role_relationship']=$value;
					}
					else
					{
						$where=array('id'=>$value);
						$arry[$i]['name']=$DB->query('role','',$where)['name'];
						$i++;
					}
				}
				$data2=array($arry,gettoken($_SESSION['id']));
				echo json_encode($data2);
			}
			else
			{
				echo false;
			}
		}
		else
		{
			echo 403;
		}
		break;
	case 'updatarelationship'://更新关系
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$where=array('id'=>$_GET['roleid']);
			$data=json_decode(file_get_contents("php://input"),true);
			if($data['relationship']=='')
			{
				return false;
			}
			else
			{
				$PJdata=array($DB->query('role','',$where,'relationship'));
				$PDdata=explode('|',$PJdata[0]['relationship']);
				$index=array_search($_GET['id'],$PDdata);
				$PDdata[$index-1]=$data['relationship'];
				$updata="";
				foreach($PDdata as $key=>$value)
				{
					if($key%2==0)
					{
						$updata=$updata.$value;
					}
					else
					{
						$updata=$updata.'|'.$value.'|';
					}
				}
				$updata=array('relationship'=>rtrim($updata,'|'));
				$DB->update('role',$updata,$where);
				$returndata=array();
				$where['id']=$_GET['roleid'];
				$Pdata=array($DB->query('role','',$where,'relationship'));
				$Pdata=explode('|',$Pdata[0]['relationship']);
				$i=0;
				foreach($Pdata as $key=>$value)
				{
					if($key%2==0)
					{
						$returndata[$i]['role_relationship']=$value;
					}
					else
					{
						$where=array('id'=>$value);
						$returndata[$i]['id']=$value;
						$returndata[$i]['name']=$DB->query('role','',$where)['name'];
						$i++;
					}
				}
				array_push($returndata,gettoken($_SESSION['id']));
			}
			echo json_encode($returndata);
		}
		else
		{
			echo 403;
		}
		break;
	case 'deleterelationship'://删除关系
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$where=array('id'=>$_GET['roleid']);
			$data=array($DB->query('role','',$where,'relationship'));
			$data=explode('|',$data[0]['relationship']);
			$index=array_search($_GET['id'],$data);
			array_splice($data,$index-1,2);
			$updata='';			
			foreach($data as $key=>$value)
			{
				if($key%2==0)
				{
					$updata=$updata.$value;
				}
				else
				{
					$updata=$updata.'|'.$value.'|';
				}
			}
			$updata=array('relationship'=>rtrim($updata,'|'));
			$DB->update('role',$updata,$where);
			$i=0;
			$returndata=array();
			foreach($data as $key=>$value)
			{
				if($key%2==0)
				{
					$returndata[$i]['role_relationship']=$value;
				}
				else
				{
					$where=array('id'=>$value);
					$returndata[$i]['id']=$value;
					$returndata[$i]['name']=$DB->query('role','',$where)['name'];
					$i++;
				}
			}
			array_push($returndata,gettoken($_SESSION['id']));
			echo json_encode($returndata);
		}
		else
		{
			echo 403;
		}
		break;
	case 'deleterole'://删除角色
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$where=array('id'=>$_GET['id']);
			$DB->del('role',$where);
			$where2=array('the_story'=>$_GET['the_story']);
			$data=array($DB->querys('role','',$where2),gettoken($_SESSION['id']));
			echo json_encode($data);
		}
		else
		{
			echo 403;
		}
		break;
	case 'editrole'://编辑角色
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$data=json_decode(file_get_contents("php://input"),true);
			$where=array('id'=>$data['data']['id']);
			if($data['data']['lead'])
			{
				$data['data']['lead']="√";
			}
			else
			{
				$data['data']['lead']="×";
			}
			$DB->update("role",$data['data'],$where);
			$where2=array('the_story'=>$_GET['storyid']);
			$returndata=array($DB->querys('role','',$where2),gettoken($_SESSION['id']));
			echo json_encode($returndata);
		}
		else
		{
			echo 403;
		}
		break;
	case 'insterattribute'://添加属性
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$data=json_decode(file_get_contents("php://input"),true);
			$where=array('id'=>$_GET['id']);
			$Pdata=array($DB->query('role','',$where,'attribute'));
			$Pdata2=explode('|',$Pdata[0]['attribute']);
			$index=0;
			if($Pdata=="")
			{
				$index=1;
			}
			else
			{
				$index=$Pdata2[sizeof($Pdata2)-3]+1;
			}
		 	if(strstr($Pdata[0]['attribute'],$data['data']['attributename'])==false)
			{
				$Pdata[0]['attribute']=$Pdata[0]['attribute'].'|'.$index.'|'.Assemblydata($data['data'],'attributename');
				$DB->update('role',$Pdata[0],$where);
				$Cdata=array($DB->query('role','',$where,'attribute'));
				$Cdata=explode('|',$Cdata[0]['attribute']);
				$returndata=array();
				$i=0;
				$a=0;
					foreach($Cdata as $key=>$value)
					{
						if($key==$a)
						{
							$returndata[$i]['id']=$value;
						}
						else
						{
							if($key==$a+1)
							{
								$returndata[$i]['property_name']=$value;
							}
							if($key==$a+2)
							{
							$returndata[$i]['content']=$value;
									$a=$a+3;
								$i++;
							}
						} 
					}
					array_push($returndata,gettoken($_SESSION['id'])); 
					echo json_encode($returndata);
				}
				else
				{
					echo json_encode(false);
				}
			}
			else
			{
				echo 403;
			}
		break;
	case 'submitattribute'://更新属性
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$data=json_decode(file_get_contents("php://input"),true);
			$where=array('id'=>$_GET['id']);
			$Pdata=array($DB->query('role','',$where,'attribute'));
			$Pdata=explode('|',$Pdata[0]['attribute']);
			$index=array_search($data['data']['id'],$Pdata);
			$Pdata[$index+1]=$data['data']['attributename'];
			$Pdata[$index+2]=$data['data']['content'];
			$PJdata="";
			foreach($Pdata as $key=>$value)
			{
				$PJdata=$PJdata.'|'.$value;
			}
			$PJdata=substr($PJdata,1);
			$updata=array('attribute'=>$PJdata);
			$DB->update('role',$updata,$where);
			$Pdata2=explode('|',$PJdata);
			$returndata=array();
			$i=0;
			$a=0;
			foreach($Pdata as $key=>$value)
			{
				if($key==$a)
				{
					$returndata[$i]['id']=$value;
				}
				else
				{
					if($key==$a+1)
					{
						$returndata[$i]['property_name']=$value;
					}
					if($key==$a+2)
					{
					$returndata[$i]['content']=$value;
							$a=$a+3;
						$i++;
					}
				} 
			}
			array_push($returndata,gettoken($_SESSION['id'])); 
			echo json_encode($returndata);
		}
		else
		{
			echo 403;
		}
		break;
	case 'deleteattribute'://删除属性
		new sqlsafe();
		if(checktoken($_GET['token']))
		{
			$where=array('id'=>$_GET['id']);
			$data=$DB->query('role','',$where,'attribute')['attribute'];
			$data=explode('|',$data);
			$index=array_search($_GET['attributeid'],$data);
			array_splice($data,$index,1);
			array_splice($data,$index,1);
			array_splice($data,$index,1);
			$LSdata="";
			foreach($data as $key=>$value)
			{
				$LSdata=$LSdata.'|'.$value;
			}
			$LSdata=substr($LSdata,1);
			$updata=array('attribute'=>$LSdata);
			$DB->update('role',$updata,$where);
			$returndata=array();
			$i=0;
			$a=0;
			foreach($data as $key=>$value)
			{
				if($key==$a)
				{
					$returndata[$i]['id']=$value;
				}
				else
				{
					if($key==$a+1)
					{
						$returndata[$i]['property_name']=$value;
					}
					if($key==$a+2)
					{
						$returndata[$i]['content']=$value;
						$a=$a+3;
						$i++;
					}
				} 
			}
			array_push($returndata,gettoken($_SESSION['id']));
			echo json_encode($returndata);
		}
		else
		{
			echo 403;
		}
		break;
	case 'roleloding':
			new sqlsafe();
			if(checktoken($_GET["token"]))
			{
				$storyid=Data_filtering($_GET["storyid"]);
				$where=array('the_story'=>$storyid);
				$arry=array($DB->querys('role','',$where),'token'=>gettoken($_SESSION['id']));
				echo json_encode($arry);
			}
			break;
	case 'RoleChart':
		new sqlsafe();
		$where=array('id'=>$_GET['id']);
		$data=array($DB->query('role','',$where,'relationship'));
		$data=explode('|',$data[0]['relationship']);
		$Cdata=array();
		$i=0;
		$Cdata[$i]['id']=$_GET['id'];
		$Cdata[$i]['parentNodeId']="";
		$Cdata[$i]['group']=$DB->query('role','',$where,'name')['name'];		
		$Cdata[$i]['nodeTitle']="";
		$Cdata[$i]['nodeNote']="";
		$Cdata[$i]['nodes']="";
		$i++;
		$a=0;
		$link=array();
		for($key=0;$key<count($data);)
		{
			$Cdata[$i]['id']=$data[$key+1];
			$Cdata[$i]['parentNodeId']=$Cdata[0]['id'];
			$where2=array('id'=>$Cdata[$i]['id']);
			$Cdata[$i]['group']=$DB->query('role','',$where2,'name')['name'].'('.$data[$key].')';
			$Cdata[$i]['nodeTitle']="";
			$Cdata[$i]['nodeNote']="";
			$Cdata[$i]['nodes']="";
			$link[$a]['target']=$Cdata[$i]['id'];
			$link[$a]['source']=$Cdata[0]['id'];
			$i++;
			$a++;
			$key=$key+2;
		}
		$returndata=array();
		$returndata['result']['nodes']=$Cdata;
		$returndata['result']['links']=$link;
		echo json_encode($returndata);
		break;
}
?>